package com.flight.trans.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.adapter.StorageAreaContainerAdapter;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.base.BaseDialog;
import com.flight.trans.bean.TugBean.TugData;
import com.flight.trans.bean.EnterPortStorageBean;
import com.flight.trans.bean.StorageAreaBean.StorageAreaData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.listener.OnSheetItemClickListener;
import com.flight.trans.listener.OnTextClickListener;
import com.flight.trans.util.DensityUtils;
import com.flight.trans.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author : YanKing
 * @date : 2019/5/19  9:53
 * @Description : 确定取消弹框
 */
public class StorageContainerDialog extends BaseDialog {
    private LinearLayout mLayoutBg;
    private RecyclerView mRlContainer;//内容
    private TextView mBtnCancel,//取消按钮
            mBtnConfirm;//确定按钮

    private boolean IsEditor ;
    private List<EnterPortStorageBean> contentList;
    private List<TugData> mTugList ;

    private StorageAreaContainerAdapter mContainerAdapter;

    private static StorageContainerDialog alertDialog ;

    public static StorageContainerDialog getInstance(Context mContext) {
        if(alertDialog == null)
            alertDialog = new StorageContainerDialog(mContext);
        return new StorageContainerDialog(mContext);
    }

    private StorageContainerDialog(Context mContext) {
        this.mContext = mContext;
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mDisplay = windowManager.getDefaultDisplay();
        builder();
    }

    private StorageContainerDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_storage_container, null);

        // 获取自定义Dialog布局中的控件
        mLayoutBg = view.findViewById(R.id.mLayoutBg);
        mRlContainer = view.findViewById(R.id.mRlContainer);
        mBtnCancel = view.findViewById(R.id.mBtnCancel);
        mBtnConfirm = view.findViewById(R.id.mBtnConfirm);

        // 定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.ActionSheetDialogStyle);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(false);

        // 调整dialog背景大小
        mLayoutBg.setLayoutParams(new FrameLayout.LayoutParams( 4 * DensityUtils.getScreenWidth((Activity) mContext) / 5, 4 * DensityUtils.getScreenHeight((Activity) mContext) / 5));

        //初始化取消按钮
        setCancelButton();
        //点击空白处可消失
        setCancelable(false);
        return this;
    }

    /**
     * 设置内容
     *
     * @param contentList
     * @return
     */
    public StorageContainerDialog setContent(List<EnterPortStorageBean> contentList , boolean IsEditor) {
        this.contentList = contentList ;
        this.IsEditor = IsEditor ;

        initStorageAreaContainerAdapter();

        return this;
    }

    /**
     * 获取
     */
    private void initStorageAreaContainerAdapter(){
        if(contentList == null || contentList.size() == 0){
            contentList = new ArrayList<>();
            contentList.add(new EnterPortStorageBean(ConstantInfo.Enter_Container , 1));
            contentList.add(null);
        }

        if(!contentList.contains(null) && IsEditor)
            contentList.add(null);

        if(mContainerAdapter == null){
            mContainerAdapter = new StorageAreaContainerAdapter(mContext , contentList , IsEditor);
            mRlContainer.setAdapter(mContainerAdapter);
            mRlContainer.setLayoutManager(new LinearLayoutManager(mContext));

            mContainerAdapter.setOnTextClickListener(new ItemTextClickListener());
            mContainerAdapter.setOnItemClickListener(new ItemClickListener());
        }else{
            mContainerAdapter.updateAdapter(contentList , IsEditor);
        }
    }

    /**
     * 条目字段点击事件
     */
    private class ItemTextClickListener implements OnTextClickListener {

        @Override
        public void onClick(View view, int position) {
            switch (view.getId()){
                case R.id.mTvContainer :
                    // 拖斗号
                    BottomDialog.getInstance(mContext)
                            .addSheetItem(ConstantInfo.ContainerList)
                            .setOnItemClickListener(new OnSheetItemClickListener() {
                                @Override
                                public void onClick(int which) {
                                    contentList.get(position).setTransportType(which + 1);

                                    // 容器编号
                                    TugDialog.getInstance(mContext)
                                            .setTugType(0)
                                            .setOnItemClickListener(new TugDialog.OnSheetItemClickListener() {
                                                @Override
                                                public void onClick(TugData tugData) {
                                                    contentList.get(position).setTransportCode(tugData.getCode());

                                                    initStorageAreaContainerAdapter();
                                                }
                                            }).show();
                                }
                            }).show();
                    break;
                case R.id.mTvStorageArea :
                    // 存放区域
                    StorageAreaDialog.getInstance(mContext , Constant.EnterPortStorage)
                            .setSingleSelect(true)
                            .setConfirmButton(new StorageAreaDialog.OnStorageOnClickListener() {
                                @Override
                                public void onClick(List<StorageAreaData> storageAreaList) {
                                    contentList.get(position).setWarehouseAreaId(storageAreaList.get(0).getId());
                                    contentList.get(position).setWarehouseAreaName(storageAreaList.get(0).getName());
                                    initStorageAreaContainerAdapter();
                                }
                            })
                            .show();
                    break;
            }
        }
    }

    /**
     * 条目点击事件
     */
    private class ItemClickListener extends OnItemClickListener{
        @Override
        public void onClick(View view, int position) {
            contentList.add(contentList.size() - 1 , new EnterPortStorageBean());
            initStorageAreaContainerAdapter();
        }

        @Override
        public void onLongClick(View view, int position) {
            AlertDialog.getInstance(mContext)
                    .setContent(R.string.dialog_remove_storage_info)
                    .setConfirmButton(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            contentList.remove(position);
                            initStorageAreaContainerAdapter();
                        }
                    }).show();
        }
    }

    /**
     * 设置是否点击空白处消失,false为不消失
     *
     * @param cancel
     * @return
     */
    public StorageContainerDialog setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    /**
     * 设置确定按钮
     *
     * @param listener
     * @return
     */
    public StorageContainerDialog setConfirmButton(final OnConfirmClickListener listener) {
        mBtnConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(contentList == null)
                    return;

                for (int i = 0; i < contentList.size(); i++) {
                    if(contentList.get(i) == null)
                        continue;

                    if(TextUtils.isEmpty(contentList.get(i).getTransportCode()) && contentList.get(i).getWarehouseAreaId() == 0){
                        ToastUtils.showToast(R.string.toast_storage_many_null);
                        return;
                    }
                }

                mDialog.dismiss();
                contentList.remove(null);
                listener.onClick(contentList);
            }
        });
        return this;
    }

    /**
     * 设置取消按钮
     *
     * @return
     */
    public StorageContainerDialog setCancelButton() {
        mBtnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
        return this;
    }

    /**
     * 设置确定按钮
     *
     * @param listener
     * @return
     */
    public StorageContainerDialog setCancelButton(final OnClickListener listener) {
        mBtnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                listener.onClick(v);
            }
        });
        return this;
    }

    public interface OnConfirmClickListener {
        void onClick(List<EnterPortStorageBean> contentList);
    }
}
